<?php

namespace app\api\controller;

use app\api\model\Setting;
use app\api\model\User as UserModel;
use app\store\model\Setting as SettingModel;
use think\Db;

/**
 * 用户管理
 * Class User
 * @package app\api
 */
class User extends Controller
{
    /**
     * 用户自动登录
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function login()
    {
        $model = new UserModel;
        return $this->renderSuccess([
            'user_id' => $model->login($this->request->post()),
            'token' => $model->getToken()
        ]);
    }
    public function login2(){
        $model = new UserModel;
        $model->test($this->request->post());
    }

    public function setting(){
        $values = SettingModel::getItem('points');
        return $this->renderSuccess(compact('values'));
    }

    /**
     * 当前用户详情
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\exception\DbException
     */
    public function detail()
    {
        // 当前用户信息
        $userInfo = $this->getUser();
        return $this->renderSuccess(compact('userInfo'));
    }

    /**
     * @title修改头像
     * @author Qian
     * Time: 2023/3/1  9:46
     */
    public function changeHead(){
        $userInfo = $this->getUser();
        $file = $this->request->file('image');

        // 移动到框架应用根目录/public/uploads/ 目录下
        $info = $file->validate(['size'=>4 * 1024 * 1024,'ext'=>'jpg,png,gif,jpeg'])->move(WEB_PATH . 'uploads/head/');

        if($info){
            //返回上传图片路径
            $path = '/uploads/head/'.$info->getSaveName();
            Db::name('user')->where(['user_id'=>$userInfo['user_id']])->update(['avatarUrl'=>$path]);
            return $this->renderSuccess(['path'=>base_url().$path],'上传成功');
        }else{
            // 上传失败获取错误信息
            return $this->renderError($file->getError());
        }
    }

    public function changeNickName($nick){
        $nick = str_replace(' ','',$nick);
        if ($nick == ''){
            return $this->renderError('请输入昵称');
        }
        if (mb_strlen($nick) >= 10){
            return $this->renderError('昵称最长10位');
        }
        $userInfo = $this->getUser();
        $nick = htmlspecialchars(strip_tags($nick));
        Db::name('user')->where(['user_id'=>$userInfo['user_id']])->update(['nickName'=>$nick]);
        return $this->renderSuccess([],'修改成功');
    }

    public function bindPhone(){
        $user = $this->getUser();
        if ($user['phone'] != ''){
            return $this->renderError('已经绑定手机号了');
        }
        $model = new UserModel();
        $result = $model->getPhone($this->request->post());
        $phone = $result['phone_info']['phoneNumber'];
        $check = UserModel::detail(['phone'=>$phone]);
        if ($check){
            return $this->renderError('此手机号已被其他用户绑定');
        }
        Db::startTrans();
        try {
            Db::name('user')->where(['user_id'=>$user['user_id']])->update(['phone'=>$phone]);
            $un_active_ids = Db::name('course_order')->where(['phone'=>$phone,'is_active'=>0,'is_delete'=>0,'pay_status'=>20,'wxapp_id'=>$user['wxapp_id']])->column('order_id');
            if ($un_active_ids){
                Db::name('course_order')->where(['order_id'=>['in',$un_active_ids]])->update(['user_id'=>$user['user_id'],'is_active'=>1,'update_time'=>time()]);
                Db::name('course_order_detail')->where(['order_id'=>['in',$un_active_ids]])->update(['user_id'=>$user['user_id'],'is_active'=>1]);
            }
            Db::commit();
            return $this->renderSuccess([],'绑定成功');
        }catch (\Exception $exception){
            Db::rollback();
            return $this->renderError('绑定失败');
        }
    }

    /**
     * @title 激活课程卡
     * @author Qian
     * Time: 2023/3/3  16:54
     */
    public function ActiveCard(){
        $user = $this->getUser();
        if ($user['phone'] == ''){
            return $this->renderError('还没有绑定手机号');
        }
        $un_active_ids = Db::name('course_order')->where(['phone'=>$user['phone'],'is_active'=>0,'is_delete'=>0,'pay_status'=>20])->column('order_id');
        if (!$un_active_ids){
            return $this->renderError('还没有待激活的课程卡');
        }
        try {
            Db::name('course_order')->where(['order_id'=>['in',$un_active_ids]])->update(['user_id'=>$user['user_id'],'is_active'=>1,'update_time'=>time()]);
            Db::name('course_order_detail')->where(['order_id'=>['in',$un_active_ids]])->update(['user_id'=>$user['user_id'],'is_active'=>1]);
            Db::commit();
            return $this->renderSuccess([],'激活'.count($un_active_ids).'张卡');
        }catch (\Exception $exception){
            Db::rollback();
            return $this->renderError('激活失败');
        }
    }
}
